import QtQuick 2.0
import QtQuick.Window 2.12
import QtCharts 2.3
import QtQuick.Controls 2.12

import "../components"

// 荧光曲线图
// 嵌入QWidget
Item {
    id: root
    anchors.fill: parent
//    objectName: "PCRChart_" + chartname

    property int loadTimes: 0

    function refresh(){

        // 从后端找要加载的图表数据
        loadTimes += 1

        console.time("QML-getChartDataTime")
        let seriesList = CAnalysis.getChartData()
        console.timeEnd("QML-getChartDataTime")


        console.time("QML-appendCurveTime")
        chart.init()
        for(let key in seriesList){
            chart.appendCurve(key, seriesList[key]["PointList"], seriesList[key]["Bold"], seriesList[key]["Color"])

        }
        if(curPage === 2){
            chart.setFixedRange("minx", -5)
            chart.setFixedRange("maxx", 5)
            chart.setFixedRange("miny", 0)
            chart.setFixedRange("maxy", 40)
        }
        console.timeEnd("QML-appendCurveTime")

        console.time("QML-RefreshTime")
        chart.refresh()
        console.timeEnd("QML-RefreshTime")

        if(curPage === 1 && checkThr.getStatus()){
            refreshThreshold()
        }

        refreshChartAxisTitle()

    }

    // 图表标题修改
    function refreshChartAxisTitle(){
        if(curPage === 0){
            chart.cAxisNameX = "循环数(Cycles)"
            chart.cAxisNameY = "荧光强度(Ru)"
        }else if(curPage === 1){
            if(curChart === 0 || curChart === 2){
                chart.cAxisNameX = "循环数(Cycles)"
                chart.cAxisNameY = "荧光强度(Rn)"
            }else{
                chart.cAxisNameX = "循环数(Cycles)"
                chart.cAxisNameY = "荧光强度(ΔRn)"
            }
        }else if(curPage === 2){
            chart.cAxisNameY = "CT"
            chart.cAxisNameX = "浓度(Log)"
        }
    }

    CustomChart{
        id: chart
        onMousePositionChanged: {
//            console.log("position")
        }onMousePressed: {
//            console.log("pressed")
        }onMouseReleased: {
//            console.log("released")
        }
    }


    property real yTop: chart.cMaxY   // 图表显示范围
    property real yBot: chart.cMinY
    property var  thrList: []
    property int curThr: -1
    property var onThrPressed: false
    property var pressedThr: -1
    function getThrIdx(mousePos){
        let minDis = layout_thr.height / 100

        let idx = -1

        if(Math.abs(thr0.y - mousePos.y) < minDis){
            idx = 0
            minDis = Math.abs(thr0.y - mousePos.y)
        }
        if(Math.abs(thr1.y - mousePos.y) < minDis){
            idx = 1
            minDis = Math.abs(thr1.y - mousePos.y)
        }
        if(Math.abs(thr2.y - mousePos.y) < minDis){
            idx = 2
            minDis = Math.abs(thr2.y - mousePos.y)
        }
        if(Math.abs(thr3.y - mousePos.y) < minDis){
            idx = 3
            minDis = Math.abs(thr3.y - mousePos.y)
        }
        return idx
    }

    // 阈值线控制
    Item{

        id: layout_thr


        visible: checkThr.check && checkThr.checkable && curPage === 1

        height: chart.height - chart.axisSizeB
        width: chart.width - chart.axisSizeL
        x: chart.axisSizeL

        PCRChartThrLine{
            id: thr0
            title: channelName[0] + ": " + thrList[0].toFixed(2).toString()
            color: "#1677FF"
            y: (1-(thrList[0]-yBot)/(yTop-yBot)) * parent.height
            onHold: curThr === 0
        }
        PCRChartThrLine{
            id: thr1
            title: channelName[1] + ": " + thrList[1].toFixed(2).toString()
            color: "#AADF57"
            y: (1-(thrList[1]-yBot)/(yTop-yBot)) * parent.height
            onHold: curThr === 1
        }
        PCRChartThrLine{
            id: thr2
            title: channelName[2] + ": " + thrList[2].toFixed(2).toString()
            color: "#FF7716"
            y: (1-(thrList[2]-yBot)/(yTop-yBot)) * parent.height
            onHold: curThr === 2
        }
        PCRChartThrLine{
            id: thr3
            title: channelName[3] + ": " + thrList[3].toFixed(2).toString()
            color: "#EF55FB"
            y: (1-(thrList[3]-yBot)/(yTop-yBot)) * parent.height
            onHold: curThr === 3
        }

        MouseArea{
            anchors.fill: parent
            cursorShape: curThr===-1 ? Qt.ArrowCursor : Qt.PointingHandCursor
            hoverEnabled: true
            onPressed: {
                if(curThr !== -1){
                    onThrPressed = true
                    pressedThr = curThr
                }
            }onPositionChanged: {
                let mousePos = mapToItem(this, mouseX, mouseY)

                if(onThrPressed){
                    mousePos.y = Math.max(mousePos.y, 0)
                    mousePos.y = Math.min(mousePos.y, layout_thr.height)
                    if(pressedThr == 0){
                        thr0.y = mousePos.y
                        thrList[0] = ((layout_thr.height-mousePos.y)/layout_thr.height) * (yTop-yBot) + yBot
                    }else if(pressedThr == 1){
                        thr1.y = mousePos.y
                        thrList[1] = ((layout_thr.height-mousePos.y)/layout_thr.height) * (yTop-yBot) + yBot
                    }else if(pressedThr == 2){
                        thr2.y = mousePos.y
                        thrList[2] = ((layout_thr.height-mousePos.y)/layout_thr.height) * (yTop-yBot) + yBot
                    }else if(pressedThr == 3){
                        thr3.y = mousePos.y
                        thrList[3] = ((layout_thr.height-mousePos.y)/layout_thr.height) * (yTop-yBot) + yBot
                    }
                    thrListChanged()
                }else{
                    curThr = getThrIdx(mousePos)
                }

            }onReleased: {
                onThrPressed = false
                if(pressedThr == 0){
                    CAnalysis.changeThr(0, thrList[0])
                }else if(pressedThr == 1){
                    CAnalysis.changeThr(1, thrList[1])
                }else if(pressedThr == 2){
                    CAnalysis.changeThr(2, thrList[2])
                }else if(pressedThr == 3){
                    CAnalysis.changeThr(3, thrList[3])
                }
                pressedThr = -1
                reloadAnalysisData()
            }
        }

    }

    function refreshThreshold(){
        thrList = CAnalysis.getThrPosi()
        thr0.y = (1-(thrList[0]-yBot)/(yTop-yBot)) * layout_thr.height
        thr1.y = (1-(thrList[1]-yBot)/(yTop-yBot)) * layout_thr.height
        thr2.y = (1-(thrList[2]-yBot)/(yTop-yBot)) * layout_thr.height
        thr3.y = (1-(thrList[3]-yBot)/(yTop-yBot)) * layout_thr.height
    }



    // 无曲线时的提示信息
    MaskInfo{
        anchors.centerIn: parent
        title: "当前数据无法计算标准曲线"
        anchors.horizontalCenterOffset: 10*dp
        anchors.verticalCenterOffset: -20*dp
        visible: curPage === 2 && (!canGuide || !canAnalysis)
    }

    MaskInfo{
        anchors.centerIn: parent
        title: "当前数据无法计算扩增曲线"
        anchors.horizontalCenterOffset: 10*dp
        anchors.verticalCenterOffset: -20*dp
        visible: curPage === 1 && (!canAnalysis)
    }
}
